// pages/pfep/baseData/ronqi/ronqi.js
const mybehavior = require("../../../../../../behaviors/requests")
const baseUrl = "https://logisticsengineering.gree.com/pfep2_api/photo_manage/"
import WxValidate from "../../../../../../Wx_Validate/WxValidate"

Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/pfep2_api",
  
    submit_name: "",
    uploader: [],
    choose_img_btn_disabled: false,
    fileList: [],
    popup_show1: false,
    options1: [],
    options11: [],
    popup_show11: false,
    options12: [],
    popup_show2: false,
    options2: ["容器库", "辅材库"],
    popup_show3: false,
    options3: [],

    popup_show4: false,
    options4: [],
    popup_show5: false,
    options5: ["A", "B", "C"],
    unique_cond: {
      content: {
        applicable_area: "",
        category: "",
        code: "",
        color: "",
        company: "",
        database: "",
        dead_weight: "",
        image_id: "",
        internal_size: "",
        material_category: "",
        material: "",
        max_load: "",
        name: "",
        size: "",
        unit: "",
        grade: "",
        plate: "",
        operate_mode: "",
        material_group: [],
        use_in: [],
        apply_category: ""
      },
      receivers: [],
      sign_module: [],
      start_process: "",
      key: "",
      unique_cond: { name: "", task_type: "供应商新增容器辅材数据申请" },
    },
    size: "",
    show1: false,
    params1: "",
    params2: "",
    params3: "",
    flag: "",
    search_value: "",
    search_value1: "",
    popup_show_bk: false,
    options_bk: [],
    popup_show_type: false,
    options_type: ["外购件", "自制件"],
    popup_show_material_category: false,
    options_material_category: [],
    popup_show_zy: false,
    options_zy: ["通用", "自动化", "人工"],
    popup_show_wlz: false,
    options_wlz: [],
    unit_list: [],
    wlz_list: [],
    disabled_flag: false,
    disabled_flag1: false,
    activeNames: ['1'],
    activeNames1: ['1'],
    business_content: "",
    creator_name: "",
    lastProcessUser: "",
    lastProcessAction: "",
    steps: [],
    active: "",
    row_data: {},
    sign_module: [
      {
        current_involved: [],
        current_process: "编制",
        sign_type: 1,
      },
      {
        current_involved: [],
        current_process: "审阅",
        sign_type: 1,
      },
      {
        current_involved: [],
        current_process: "审阅",
        sign_type: 0,
      },
      {
        current_involved: [],
        current_process: "会签",
        sign_type: 0,
      },
      {
        current_involved: [],
        current_process: "标准化",
        sign_type: 0,
      },
      {
        current_involved: [],
        current_process: "批准",
        sign_type: 0,
      },
      {
        current_involved: [],
        current_process: "接收",
        sign_type: 0,
      },

    ],
    handle_reason: "",
    inspect_show: false,
    inspect_show2: false,
    handle_action: ""
  },
  onActiveChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  onActiveChange1(event) {
    this.setData({
      activeNames1: event.detail,
    });
  },
  onSearch1(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options11
      this.setData({
        options1: arr
      })
    } else {
      let list1 = this.data.options11
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options1: arr
      })
    }
  },
  onSearch11(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options11
      this.setData({
        options12: arr
      })
    } else {
      let list1 = this.data.options11
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value11) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options12: arr
      })
    }
  },
  bindClickIcon(e) {
    console.log(e.currentTarget.dataset.icon);
    wx.showToast({
      title: e.currentTarget.dataset.icon,
      icon: "none"

    })
  },
  // 点击--规格名称--选取输入框
  onClick14(e) {
    // event.detail 为当前输入的值
    console.log(e.currentTarget.dataset.name);
    this.setData({
      show1: true,
      flag: e.currentTarget.dataset.name
    })
  },
  // 生成--规格名称
  onConfirm1() {
    if (this.data.params1 != "" && this.data.params1 != "") {
      let val = this.data.params1 + "*" + this.data.params2 + "*" + this.data.params3
      let key = "unique_cond.content." + this.data.flag

      console.log(key);

      this.setData({
        show1: false,
        [key]: val,

      })
      let key1 = "unique_cond.content.name"

      let key11 = this.data.unique_cond.content.material_category + this.data.unique_cond.content.category + this.data.unique_cond.content.size + this.data.unique_cond.content.material + this.data.unique_cond.content.color

      this.setData({
        [key1]: key11,
        "unique_cond.unique_cond.name": key11,


      })
    }

  },
  // 生成--规格名称
  onClose11() {
    console.log("123");
    this.setData({
      show1: false,
      params1: "",
      params2: "",
      params3: "",
      flag: "",
    })
  },

  // 获取--容器辅材类别--数据
  getTypeDatas(e) {

    let url = this.data.baseUrl + "/unities_manage/query_unities "
    let data = {
      unity_type: e,
      name: "",
    }
    this.post(url, data).then(res => {
      console.log("容器辅材类别", res)
      let data_list = res.data


      if (res.code == 20000) {

        this.setData({
          options3: data_list,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 获取计量单位数据
  getUnitDatas() {

    let url = this.data.baseUrl + "/unities_manage/query_unities "
    let data = {
      unity_type: "计数单位",
      name: "",
    }
    this.post(url, data).then(res => {
      console.log("获取计量单位数据", res)
      let data_list = res.data


      if (res.code == 20000) {

        this.setData({
          options4: data_list,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 获取板块数据
  getBankuaiDatas() {

    let url = this.data.baseUrl + "/unities_manage/query_unities "
    let data = {
      unity_type: "板块",
      name: "",
    }
    this.post(url, data).then(res => {
      console.log("获取板块数据", res)
      let data_list = res.data


      if (res.code == 20000) {

        this.setData({
          options_bk: data_list,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 照片选取
  try_upload(e) {
    const self = this;
    self.setData({
      choose_img_btn_disabled: true
    })
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        console.log(tempFilePath);
        wx.showLoading({
          title: '开始上传。。。',
        })

        wx.uploadFile({
          url: "https://logisticsengineering.gree.com/pfep2_api/photo_manage/fs_upload",
          filePath: tempFilePath,
          name: 'file',
          formData: {
          },
          success(res) {
            const result = JSON.parse(res.data);
            console.log(result)
            wx.hideLoading();
            if (result.code == 20000) {
              const uploader = self.data.uploader;
              const new_uploader = uploader.concat({
                url: baseUrl + "fs_download?file_id=" + result.data.file_id,
                isImage: true,
                file_id: result.data.file_id
              })

              self.setData({
                uploader: new_uploader,

              })
              if (self.data.uploader.length < 3) {
                self.setData({
                  choose_img_btn_disabled: false
                })
              }
              wx.showToast({
                title: '上传成功！',
              })
            } else {
              if (self.data.uploader.length < 3) {
                self.setData({
                  choose_img_btn_disabled: false
                })
              }
              wx.showToast({
                title: result.msg,
                icon: "error"
              })
            }

          },
          fail(res) {
            wx.hideLoading();
            if (self.data.uploader.length < 3) {
              self.setData({
                choose_img_btn_disabled: false
              })
            }
            wx.showToast({
              title: "未知错误428",
              icon: "error"
            })
          }
        })

      },
      fail() {
        if (self.data.uploader.length < 3) {
          self.setData({
            choose_img_btn_disabled: false
          })
        }
      }



    })
  },
  handle_delete(e) {
    const index = e.detail.index;
    const file = this.data.uploader[index];
    const file_id = file.url.split("=")[1];
    const uploader = this.data.uploader;
    const self = this;

    let url1 = baseUrl + 'fs_delete'
    let data = {
      file_id
    }
    this.post(url1, data).then(res => {
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
          icon: 'success'
        })

        uploader.splice(index, 1);
        self.setData({
          choose_img_btn_disabled: false,
        })
        self.setData({
          uploader
        });


      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  // 点击--板块--选取输入框
  onBKClick(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show_bk: true
    })
  },
  // 选择--板块--输入框关闭
  onBKClose() {
    this.setData({ popup_show_bk: false });
  },
  // 确认--板块--弹出框关闭
  onBKChange(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      "unique_cond.content.plate": value,
      popup_show_bk: false,
    })
  },


  // 点击--适用类别--选取输入框
  onTypeClick(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show_type: true
    })
  },
  // 选择--适用类别--输入框关闭
  onTypeClose() {
    this.setData({ popup_show_type: false });
  },
  // 确认--适用类别--弹出框关闭
  onTypeChange(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      "unique_cond.content.apply_category": value,
      popup_show_type: false,
    })
  },

  // 获取物料类别数据
  getMCDatas() {

    let url = this.data.baseUrl + "/unities_manage/query_unities "
    let data = {
      unity_type: "物料类别",
      name: "",
    }
    this.post(url, data).then(res => {
      console.log("获取物料类别数据", res)
      let data_list = res.data


      if (res.code == 20000) {

        this.setData({
          options_material_category: data_list,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 点击--物料类别--选取输入框
  onMCClick(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show_material_category: true
    })
  },
  // 选择--物料类别--输入框关闭
  onMCClose() {
    this.setData({ popup_show_material_category: false });
  },
  // 确认--物料类别--弹出框关闭
  onMCChange(event) {
    const { picker, value, index } = event.detail;
    let key = value + this.data.unique_cond.content.category + this.data.unique_cond.content.size + this.data.unique_cond.content.material + this.data.unique_cond.content.color
    this.setData({
      "unique_cond.content.material_category": value,
      "unique_cond.content.name": key,
      "unique_cond.unique_cond.name": key,

      popup_show_material_category: false,
    })
  },


  // 点击--作业方式--选取输入框
  onZYClick(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show_zy: true
    })
  },
  // 选择--作业方式--输入框关闭
  onZYClose() {
    this.setData({ popup_show_zy: false });
  },
  // 确认--作业方式--弹出框关闭
  onZYChange(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      "unique_cond.content.operate_mode": value,
      popup_show_zy: false,
    })
  },
  selectWLZ(e) {
    console.log(e.detail);
    let array = e.detail
    let res = []
    for (let i = 0; i < array.length; i++) {
      res.push(array[i].split('-')[0])
    }
    this.setData({
      "unique_cond.content.material_group": res,
      material_group_list: e.detail
    })
  },
  // 获取--物料组--数据
  getWLZDatas(e) {

    let url = this.data.baseUrl + "/folders_manage/query_sub_dir_managers "
    let data = {

    }
    this.post(url, data).then(res => {
      console.log("物料组数据", res)
      let data_list = res.data
      const res_arr = Object.entries(data_list).map(([key, value]) => `${key}-${value}`);
      if (res.code == 20000) {

     
        setTimeout(() => {
          let wlz_arr1 = res_arr
          let wlz_arr2 = this.data.row_data.task.content.material_group
          const intersection = wlz_arr1.filter(item => wlz_arr2.includes(item.split('-')[0]));
          this.setData({
            options_wlz: res_arr,
            material_group_list: intersection
          })
        }, 500)

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 点击--物料组--选取输入框
  onWLZClick(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show_wlz: true
    })
  },
  // 选择--物料组--输入框关闭
  onWLZClose() {
    this.setData({ popup_show_wlz: false });
  },

  // 确认--物料组--值--弹出框关闭
  onWLZChange(event) {
    const { picker, value, index } = event.detail;
    console.log("13212", value);
    let arr = this.data.wlz_list
    console.log("xxx", arr);
    if (arr.includes(value)) {
      wx.showToast({
        title: '请问重复添加',
        icon: 'error'
      })
    } else {
      arr.push(value)

    }
    this.setData({
      wlz_list: arr,
      'unique_cond.content.material_group': arr
      // "row_data.infoData.apply_unit": value,
    })
  },
  onCloseWLZ(e) {
    console.log(e.currentTarget.dataset.row_data);
    let val = e.currentTarget.dataset.row_data
    let arr = this.data.wlz_list
    for (let i in arr) {
      if (arr[i] == val) {

        arr.splice(i, 1)

      }
    }
    this.setData({
      wlz_list: arr,
      'unique_cond.content.material_group': arr
    })
  },


  // 点击--公司名称--选取输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择--公司名称--输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  // 确认--公司名称值--弹出框关闭
  onChange1(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      "unique_cond.content.company": value,
      popup_show1: false,
    })
  },




  // 多个适用单位提交
  unitSubmits() {
    if (this.data.unit_list.length == 0) {
      wx.showToast({
        title: '请选择适用单位，并确认信息填写完整',
        icon: 'none'
      })
    } else {
      for (let i in this.data.unit_list) {
        this.submit(this.data.unit_list[i])
      }
      this.setData({
        unit_list: [],
        standardData: {
          container: [],
          auxiliayMaterial: [],
          operationDescription: "",
          operationImageList: [

          ],
          identification: "",
          identificationImageList: [

          ],
          endingPackage: [
            {
              key: "s" + new Date().getTime(),
              order: "",
              imageList: [

              ]
            },

          ],

        },
        params1: "",
        params2: "",
        params3: "",
        params4: "",
        ronqi_index: null,
        unit_list: [],

        // infoData
        name: "",
        material_group: "",
        material_class: "",
        material_code: "",
        apply_unit: wx.getStorageSync('company'),

        // 容器库
        container: [
          {
            key: "s" + new Date().getTime(),
            容器名称: "",
            容器类别: "",
            容器编码: "",
            容器规格: "",
            用量: "",
          },

        ],
        // 辅材库
        auxiliayMaterial: [
          {
            key: "s" + new Date().getTime(),
            用量: "",
            辅材名称: "",
            辅材类别: "",
            辅材编码: "",
            辅材规格: "",
          }
        ],
        // packageData
        packageData: {
          单个物料重量d: "",
          单箱包装数b: "",
          单箱毛重: 0,
          容器自重e: 0,
          最小包装数a: "",
          每箱数c: 0,
          计量单位: "",
        },
        uploader: [],
        choose_img_btn_disabled: false,
        fileList: [],
        uploader1: [],
        choose_img_btn_disabled1: false,
        fileList1: [],
        uploader2: [],
        choose_img_btn_disabled2: false,
        fileList2: [],
        tb_key_list: [],

      })
    }

  },

  // 点击--适用单位--选取输入框
  onClick11(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show11: true
    })
  },
  // 选择--适用单位--输入框关闭
  onClose12() {
    this.setData({ popup_show11: false });
  },
  // 确认--适用单位--值--弹出框关闭
  onChange11(event) {
    const { picker, value, index } = event.detail;
    console.log("13212", value);
    let arr = this.data.unit_list
    console.log("xxx", arr);
    if (arr.includes(value)) {
      wx.showToast({
        title: '请问重复添加',
        icon: 'error'
      })
    } else {
      arr.push(value)

    }
    this.setData({
      unit_list: arr,
      'unique_cond.content.use_in': arr
      // "row_data.infoData.apply_unit": value,
    })
  },
  onCloseUnit(e) {
    console.log(e.currentTarget.dataset.row_data);
    let val = e.currentTarget.dataset.row_data
    let arr = this.data.unit_list
    for (let i in arr) {
      if (arr[i] == val) {

        arr.splice(i, 1)

      }
    }
    this.setData({
      unit_list: arr,
      'unique_cond.content.use_in': arr
    })
  },






  // 点击--所在库--选取输入框
  onClick2(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show2: true
    })
  },
  // 选择--所在库--输入框关闭
  onClose2() {
    this.setData({ popup_show2: false });
  },
  // 确认--所在库--弹出框关闭
  onChange2(event) {
    const { picker, value, index } = event.detail;
    console.log(value);
    if (value == "容器库") {
      this.setData({
        "unique_cond.content.database": value,
        popup_show2: false,
      })
      let val = "容器辅材类别-容器"
      this.getTypeDatas(val)

    } else {
      this.setData({
        "unique_cond.content.database": value,
        popup_show2: false,
      })
      let val = "容器辅材类别-辅材"
      this.getTypeDatas(val)
    }

  },

  // 点击--容器辅材类别--选取输入框
  onClick3(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show3: true
    })
  },
  // 选择--容器辅材类别--输入框关闭
  onClose3() {
    this.setData({ popup_show3: false });
  },
  // 确认--容器辅材类别--弹出框关闭
  onChange3(event) {
    const { picker, value, index } = event.detail;
    let key = value + this.data.unique_cond.content.category + this.data.unique_cond.content.size + this.data.unique_cond.content.material + this.data.unique_cond.content.color
    this.setData({
      "unique_cond.content.category": value,
      "unique_cond.content.name": key,
      "unique_cond.unique_cond.name": key,

      popup_show3: false,
    })
  },

  // 点击--计数单位--选取输入框
  onClick4(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show4: true
    })
  },
  // 选择--计数单位--输入框关闭
  onClose4() {
    this.setData({ popup_show4: false });
  },
  // 确认--计数单位--弹出框关闭
  onChange4(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      "unique_cond.content.unit": value,
      popup_show4: false,
    })
  },
  // 点击--容器辅材等级--选取输入框
  onClick5(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show5: true
    })
  },
  // 选择--容器辅材等级--输入框关闭
  onClose5() {
    this.setData({ popup_show5: false });
  },
  // 确认--容器辅材等级--弹出框关闭
  onChange5(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      "unique_cond.content.grade": value,
      popup_show5: false,
    })
  },
  bindInput(e) {
    console.log("123");
    // 表单双向数据绑定
    let that = this;
    let dataset = e.currentTarget.dataset;
    let name = dataset.name;

    let value = e.detail;
    // 拼接对象属性名，用于为对象属性赋值
    let attributeName = 'unique_cond.content.' + name
    that.setData({
      [attributeName]: value
    });
    console.log(attributeName);
    if (attributeName == 'unique_cond.content.size' || attributeName == 'unique_cond.content.category' || attributeName == 'unique_cond.content.material' || attributeName == 'unique_cond.content.material_category' || attributeName == 'unique_cond.content.color') {
      console.log("xxx");
      let key = that.data.unique_cond.content.material_category + that.data.unique_cond.content.category + that.data.unique_cond.content.size + that.data.unique_cond.content.material + that.data.unique_cond.content.color
      console.log("asdsd", key);
      that.setData({
        'unique_cond.content.name': key,
        "unique_cond.unique_cond.name": key,

      });
    }

    console.log(that.data[name])
  },
  // 获取--供应商--数据
  getPermissionDatas() {
    let url = this.data.baseUrl + "/data_auth_manage/get_u_permission"
    let data = {

    }
    this.post(url, data).then(res => {
      // console.log("获取供应商数据", res.data)
      let data_list = Object.keys(res.data)
      // console.log("xxx", data_list);

      if (res.code == 20000) {

        this.setData({
          options11: data_list,
          options1: data_list,
          options12: data_list

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  resetBtn() {
    this.setData({
      unique_cond: {
        content: {
          applicable_area: "",
          category: "",
          code: "",
          color: "",
          company: "",
          database: "",
          dead_weight: "",
          image_id: [],
          internal_size: "",
          material_category: "",
          material: "",
          max_load: "",
          name: "",
          size: "",
          unit: "",
          grade: "",
          plate: "",
          operate_mode: "",
          material_group: [],
          use_in: [],
          apply_category: ""
        },
        receivers: [],
        sign_module: [],
        start_process: "",
        supply: "",
        unique_cond: { name: "", task_type: "新增容器辅材数据" },
        use_in: "",
      },
    })
  },
  // 提交按钮
  submit() {

    wx.showLoading({
      title: '提交中'
    })
    let that = this
    console.log(this.data.unique_cond.content);
    let data = this.data.unique_cond.content
    let img_arr = this.data.uploader
    let img_arr1 = []
    if (img_arr.length != 0) {
      for (let i in img_arr) {
        let obj = {
          url: "http://10.2.11.67/pfep2_api/photo_manage/fs_download?file_id=" + img_arr[i].file_id,
          isImage: true,
          file_id: img_arr[i].file_id
        }
        img_arr1.push(obj)
      }
    }
    this.setData({
      'unique_cond.content.image_id': img_arr1,
    })
    if (!that.WxValidate.checkForm(data)) {
      const error = that.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
    } else {
      let paload = this.data.unique_cond

      console.log("111", paload);
      let url = this.data.baseUrl + "/supply_container_manage/correct_supply_container"
      this.post(url, paload).then(res => {
        console.log(res);
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          setTimeout(function () {
            wx.hideLoading()
            wx.reLaunch({
              url: '/packageB/pages/package/task_manager/task_manager',
            })
          }, 2000)



        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        wx.hideLoading()
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      // company: {
      //   required: true,
      // },
      // database: {
      //   required: true,
      // },
      // category: {
      //   required: true,
      // },
      // material_category: {
      //   required: true,
      // },
      // internal_size: {
      //   required: true,
      // },
      // size: {
      //   required: true,
      // },
      // name: {
      //   required: true,
      // },
      // color: {
      //   required: true,
      // },
      // unit: {
      //   required: true,
      // },
      // dead_weight: {
      //   required: true,
      // },
      // image_id: {
      //   required: true,
      // },

      // grade: {
      //   required: true,
      // },

    }
    let message = {
      // company: {
      //   required: '公司名称-不能为空',
      // },
      // database: {
      //   required: '所在库-不能为空',
      // },
      // category: {
      //   required: '容器辅材类别-不能为空',
      // },
      // material_category: {
      //   required: '物料类别-不能为空',
      // },
      // internal_size: {
      //   required: '内部有效尺寸-不能为空',
      // },

      // size: {
      //   required: '规格-不能为空',
      // },
      // name: {
      //   required: '名称-不能为空',
      // },
      // color: {
      //   required: '颜色-不能为空',
      // },
      // unit: {
      //   required: '计数单位-不能为空',
      // },
      // dead_weight: {
      //   required: '自重-不能为空',
      // },
      // image_id: {
      //   required: '图片-不能为空',
      // },

      // grade: {
      //   required: '容器辅材等级-不能为空',
      // },

    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },


  // 审批同意
  inspectShow(e) {
    wx.requestSubscribeMessage({
      tmplIds: this.getTmplIds(),

    })
    let key = e.currentTarget.dataset.flag
    console.log(key);
    this.setData({
      inspect_show: true,
      handle_action: key
    })
  },
  oninspecClose() {
    this.setData({
      inspect_show: false,
      handle_action: "",
      handle_reason: "",
    })
  },

  inspectConfirm() {

    let row = this.data.row_data


    const paload = {
      action: this.data.handle_action,
      key: row.task_id,
      backoff: 0,
      creator_id: row.task.creator_id,
      process: row.current_process,
      reason: this.data.handle_reason,
      sign_module: row.task.sign_module,

      unique_cond: {
        name: row.task.name,
        task_type: row.task.task_type,
      },
    }
    console.log(paload);
    let url = this.data.baseUrl + "/supply_container_manage/inspect_supply_container"
    this.post(url, paload).then(res => {
      console.log(res);
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/packageB/pages/package/task_manager/task_manager',
          })
        }, 2000)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPermissionDatas()
    this.getUnitDatas()
    this.getBankuaiDatas()
    this.getMCDatas()
    this.getWLZDatas()
    this.initValidate()
    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('toHandle', function (data) {
      console.log("传过来的数据", data)

      let arr1 = data.task.content.image_id
      let arr2 = []
      for (let i in arr1) {
        const new_uploader = {
          url: baseUrl + "fs_download?file_id=" + arr1[i].file_id,
          isImage: true,
          file_id: arr1[i].file_id

        }
        arr2.push(new_uploader)
      }
      if (data.current_process == "编制") {
        self.setData({
          disabled_flag: true,
        })
      }
      if (data.current_process != "编制" && data.status != "done") {
        self.setData({
          disabled_flag1: true,
          choose_img_btn_disabled: true
        })
      }
      if (data.flag == "done") {
        self.setData({
          disabled_flag: false,
          disabled_flag1: false,
          choose_img_btn_disabled: true
        })
      }
      let steps_arr = []
      for (let i in data.task.recorders) {
        data.task.recorders[i].action = data.task.recorders[i].action == "agree" ? "同意" : "不同意"
        if (data.task.recorders[i].reason == "未签") {
          let obj = {
            text: '【' + data.task.recorders[i].process + '】',
            desc: data.task.recorders[i].name + '  -  ' + data.task.recorders[i].reason,
          }
          steps_arr.push(obj)
        } else {
          let obj = {
            text: '【' + data.task.recorders[i].process + '】',
            desc: data.task.recorders[i].name + ' 在 ' + data.task.recorders[i].time + ' ' + ' ' + data.task.recorders[i].action + '办理，意见反馈：' + data.task.recorders[i].reason,
          }
          steps_arr.push(obj)
        }
      }
      if (data.task.content.opinions !== undefined) {
        steps_arr[0] = {
          text: '【编制】',
          desc: data.task.recorders[0].name + ' 在 ' + data.task.recorders[0].time + ' 提交'  + ' ' + ' 办理意见：' + data.task.content.opinions[data.creator_name]
        }
      }else{
        steps_arr[0] = {
          text: '【编制】',
          desc: data.task.recorders[0].name + ' 在 ' + data.task.recorders[0].time + ' 提交'  
        }
      }
      for (let i in data.task.recorders) {
        if (data.task.recorders[i].action == "同意") {

          self.setData({
            active: i,
            lastProcessUser:data.task.recorders[i].name,
            lastProcessAction:data.task.recorders[i].reason,
          })
        }
      }

      self.setData({
        row_data: data,
        'unique_cond.content': data.task.content,
        'unique_cond.supply': data.task.supply,
        'unique_cond.start_process': "标准化",
        'unique_cond.sign_module': [],
        'unique_cond.unique_cond.name': data.task.name,
        'unique_cond.unique_cond.task_type': data.task.task_type,
        uploader: arr2,
        'unique_cond.key': data.task_id,
        business_content: data.business_content,
        creator_name: data.creator_name,
        steps: steps_arr,


      })


    })
    eventChannel.on('showDetail', function (data) {
        console.log("传过来的数据", data)
  
        let arr1 = data.task.content.image_id
        let arr2 = []
        for (let i in arr1) {
          const new_uploader = {
            url: baseUrl + "fs_download?file_id=" + arr1[i].file_id,
            isImage: true,
            file_id: arr1[i].file_id
  
          }
          arr2.push(new_uploader)
        }
        if (data.current_process == "编制") {
          self.setData({
            disabled_flag: true,
          })
        }
        if (data.current_process != "编制" && data.status != "done") {
          self.setData({
            disabled_flag1: true,
            choose_img_btn_disabled: true
          })
        }
        if (data.flag == "done") {
          self.setData({
            disabled_flag: false,
            disabled_flag1: false,
            choose_img_btn_disabled: true
          })
        }
        let steps_arr = []
        for (let i in data.task.recorders) {
          data.task.recorders[i].action = data.task.recorders[i].action == "agree" ? "同意" : "不同意"
          if (data.task.recorders[i].reason == "未签") {
            let obj = {
              text: '【' + data.task.recorders[i].process + '】',
              desc: data.task.recorders[i].name + '  -  ' + data.task.recorders[i].reason,
            }
            steps_arr.push(obj)
          } else {
            let obj = {
              text: '【' + data.task.recorders[i].process + '】',
              desc: data.task.recorders[i].name + ' 在 ' + data.task.recorders[i].time + ' ' + ' ' + data.task.recorders[i].action + '办理，意见反馈：' + data.task.recorders[i].reason,
            }
            steps_arr.push(obj)
          }
        }
        if (data.task.content.opinions !== undefined) {
          steps_arr[0] = {
            text: '【编制】',
            desc: data.task.recorders[0].name + ' 在 ' + data.task.recorders[0].time + ' 提交'  + ' ' + ' 办理意见：' + data.task.content.opinions[data.creator_name]
          }
        }else{
          steps_arr[0] = {
            text: '【编制】',
            desc: data.task.recorders[0].name + ' 在 ' + data.task.recorders[0].time + ' 提交'  
          }
        }
        for (let i in data.task.recorders) {
          if (data.task.recorders[i].action == "同意") {
  
            self.setData({
              active: i,
              lastProcessUser:data.task.recorders[i].name,
              lastProcessAction:data.task.recorders[i].reason,
            })
          }
        }
  
        self.setData({
          row_data: data,
          'unique_cond.content': data.task.content,
          'unique_cond.supply': data.task.supply,
          'unique_cond.start_process': "标准化",
          'unique_cond.sign_module': [],
          'unique_cond.unique_cond.name': data.task.name,
          'unique_cond.unique_cond.task_type': data.task.task_type,
          uploader: arr2,
          'unique_cond.key': data.task_id,
          business_content: data.business_content,
          creator_name: data.creator_name,
          steps: steps_arr,
  
  
        })
  
  
      })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    // let gs = wx.getStorageSync('company')
    // this.setData({
    //   ["unique_cond.content.company"]: gs
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})